#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int reverse(int x) {
        string s1(to_string(x));
        if (s1[0] == '-')
            s1.erase(0, 1);

        for (int i = s1.size() - 1; i >= 0; --i)
        {
            if (s1[i] == '0')
                s1.resize(i);
            else
                break;
        }
        if (s1.size() == 0)
            return 0;

        std::reverse(s1.begin(), s1.end());
        int flag = x < 0 ? -1 : 1;
        if (flag == 1)
        {
            string tmp(to_string(INT_MAX));
            if (s1.size() > tmp.size() || ((s1.size() == tmp.size()) && s1 > tmp))
                return 0;
        }
        else
        {
            string tmp(to_string(INT_MIN));
            tmp.erase(0, 1);
            if (s1.size() > tmp.size() || ((s1.size() == tmp.size()) && s1 > tmp))
                return 0;
        }

        return flag * stoi(s1);
    }
};